1
2
3
4
5
6
7 package io.vavr.test;
8
9
10
11
12
13 import static org.assertj.core.api.Assertions.assertThat;
14
15 import io.vavr.CheckedFunction2;
16 import org.junit.Test;
17
18 public class PropertyCheck2Test {
19
20 static final Arbitrary<Object> OBJECTS = Gen.of(null).arbitrary();
21
22 @Test
23 public void shouldApplyForAllOfArity2() {
24 final Property.ForAll2<Object, Object> forAll = Property.def("test").forAll(null, null);
25 assertThat(forAll).isNotNull();
26 }
27
28 @Test
29 public void shouldApplySuchThatOfArity2() {
30 final Property.ForAll2<Object, Object> forAll = Property.def("test").forAll(OBJECTS, OBJECTS);
31 final CheckedFunction2<Object, Object, Boolean> predicate = (o1, o2) -> true;
32 final Property.Property2<Object, Object> suchThat = forAll.suchThat(predicate);
33 assertThat(suchThat).isNotNull();
34 }
35
36 @Test
37 public void shouldCheckTrueProperty2() {
38 final Property.ForAll2<Object, Object> forAll = Property.def("test").forAll(OBJECTS, OBJECTS);
39 final CheckedFunction2<Object, Object, Boolean> predicate = (o1, o2) -> true;
40 final CheckResult result = forAll.suchThat(predicate).check();
41 assertThat(result.isSatisfied()).isTrue();
42 assertThat(result.isExhausted()).isFalse();
43 }
44
45 @Test
46 public void shouldCheckFalseProperty2() {
47 final Property.ForAll2<Object, Object> forAll = Property.def("test").forAll(OBJECTS, OBJECTS);
48 final CheckedFunction2<Object, Object, Boolean> predicate = (o1, o2) -> false;
49 final CheckResult result = forAll.suchThat(predicate).check();
50 assertThat(result.isFalsified()).isTrue();
51 }
52
53 @Test
54 public void shouldCheckErroneousProperty2() {
55 final Property.ForAll2<Object, Object> forAll = Property.def("test").forAll(OBJECTS, OBJECTS);
56 final CheckedFunction2<Object, Object, Boolean> predicate = (o1, o2) -> { throw new RuntimeException("yay! (this is a negative test)"); };
57 final CheckResult result = forAll.suchThat(predicate).check();
58 assertThat(result.isErroneous()).isTrue();
59 }
60
61 @Test
62 public void shouldCheckProperty2ImplicationWithTruePrecondition() {
63 final Property.ForAll2<Object, Object> forAll = Property.def("test").forAll(OBJECTS, OBJECTS);
64 final CheckedFunction2<Object, Object, Boolean> p1 = (o1, o2) -> true;
65 final CheckedFunction2<Object, Object, Boolean> p2 = (o1, o2) -> true;
66 final CheckResult result = forAll.suchThat(p1).implies(p2).check();
67 assertThat(result.isSatisfied()).isTrue();
68 assertThat(result.isExhausted()).isFalse();
69 }
70
71 @Test
72 public void shouldCheckProperty2ImplicationWithFalsePrecondition() {
73 final Property.ForAll2<Object, Object> forAll = Property.def("test").forAll(OBJECTS, OBJECTS);
74 final CheckedFunction2<Object, Object, Boolean> p1 = (o1, o2) -> false;
75 final CheckedFunction2<Object, Object, Boolean> p2 = (o1, o2) -> true;
76 final CheckResult result = forAll.suchThat(p1).implies(p2).check();
77 assertThat(result.isSatisfied()).isTrue();
78 assertThat(result.isExhausted()).isTrue();
79 }
80
81 @Test(expected = IllegalArgumentException.class)
82 public void shouldThrowOnProperty2CheckGivenNegativeTries() {
83 Property.def("test")
84 .forAll(OBJECTS, OBJECTS)
85 .suchThat((o1, o2) -> true)
86 .check(Checkable.RNG.get(), 0, -1);
87 }
88
89 @Test
90 public void shouldReturnErroneousProperty2CheckResultIfGenFails() {
91 final Arbitrary<Object> failingGen = Gen.fail("yay! (this is a negative test)").arbitrary();
92 final CheckResult result = Property.def("test")
93 .forAll(failingGen, OBJECTS)
94 .suchThat((o1, o2) -> true)
95 .check();
96 assertThat(result.isErroneous()).isTrue();
97 }
98
99 @Test
100 public void shouldReturnErroneousProperty2CheckResultIfArbitraryFails() {
101 final Arbitrary<Object> failingArbitrary = size -> { throw new RuntimeException("yay! (this is a negative test)"); };
102 final CheckResult result = Property.def("test")
103 .forAll(failingArbitrary, OBJECTS)
104 .suchThat((o1, o2) -> true)
105 .check();
106 assertThat(result.isErroneous()).isTrue();
107 }
108 }